package com.yuzhitong.shapi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.activity.WebPageActivity;
import com.yuzhitong.shapi.base.BaseMvpFragment;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.base.recycler.BaseAdapter;
import com.yuzhitong.shapi.base.recycler.BaseViewHolder;
import com.yuzhitong.shapi.bean.SearchMovieBean;
import com.yuzhitong.shapi.contract.SearchMovieContract;
import com.yuzhitong.shapi.presenter.SearchMoviePresenter;
import com.yuzhitong.shapi.widget.LoopBackgroundView;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/5/10  20:14
 * Description:
 * 根据 name 条件搜索
 */
public class SearchMovieFragment extends BaseMvpFragment<SearchMoviePresenter> implements SearchMovieContract.View {
    private RecyclerView rvSearchCategory;

    private TextView bottomTextView;

    private BaseAdapter<SearchMovieBean> adapter;

    private String searchName;
    private LoopBackgroundView lvLoading;

    public static SearchMovieFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        SearchMovieFragment fragment = new SearchMovieFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_search_movie);
        rvSearchCategory = findViewById(R.id.rv_search_movie_list);
        lvLoading = (LoopBackgroundView) findViewById(R.id.lv_loading);
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            searchName = getArguments().getString("name");
        }

        rvSearchCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvSearchCategory.setAdapter(adapter = new BaseAdapter<SearchMovieBean>() {
            @Override
            public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.item_search_movie_result, parent, false);
                return new BaseViewHolder(itemView);
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int RealPosition, SearchMovieBean data) {
                TextView tvMovieName = viewHolder.findViewById(R.id.tv_movie_name);
                TextView tvMovieUrl = viewHolder.findViewById(R.id.tv_movie_url);
                TextView tvMovieUrlType = viewHolder.findViewById(R.id.tv_movie_url_type);

                tvMovieName.setText(data.getName());
                tvMovieUrl.setText(data.getLinkUrl());
                tvMovieUrlType.setText(data.getTitle());
            }
        });

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<SearchMovieBean>() {
            @Override
            public void onItemClick(int position, SearchMovieBean data) {
                Intent intent = new Intent(getActivity(), WebPageActivity.class);
                intent.putExtra(Contents.INTENT_H5_URL, data.getLinkUrl());
                startActivity(intent);
            }
        });

        View bottomView = LayoutInflater.from(getActivity()).inflate(R.layout.item_no_more_data, null, false);
        bottomTextView = bottomView.findViewById(R.id.tv_no_more_data_text);
        adapter.setBottomView(bottomView);

        lvLoading.startLoop();
        loadData();
    }

    public void setSearchName(String searchName) {
        if (searchName != null) {
            this.searchName = searchName;
            loadData();
        }
    }

    @Override
    public void showLoading() {
        lvLoading.setVisibility(View.VISIBLE);
        bottomTextView.setText("正在搜索全网资源");
        bottomTextView.setTextSize(16);
    }

    @Override
    public void hideLoading() {
        lvLoading.setVisibility(View.GONE);
        bottomTextView.setText(R.string.list_no_more_data);
    }

    private void loadData() {
        if (searchName == null) {
            return;
        }
        if (presenter == null) {
            return;
        }
        presenter.getMovieList(searchName);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        lvLoading.stopLoop();
    }

    @Override
    protected void initPresenter() {
        presenter = new SearchMoviePresenter();
        presenter.attachView(this);
    }

    @Override
    public void showMovieList(List<SearchMovieBean> movieBeans) {
        adapter.setDataList(movieBeans);
        adapter.notifyDataSetChanged();
    }
}
